package com.marc.eden;

import android.graphics.Bitmap;

import com.baidu.aip.face.AipFace;

import org.json.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;

public class Face {

    private static final String APP_ID = Utils.APP_ID;
    private static final String API_KEY = Utils.API_KEY;
    private static final String SECRET_KEY = Utils.SECRET_KEY;
    private AipFace client = null;

    public Face() {
        init();
    }

    private boolean init() {
        client = new AipFace(APP_ID, API_KEY, SECRET_KEY);
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);
        if (client == null) return false;
        return true;
    }

    public void detect(Bitmap bitmap) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] bytes;
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        bytes = baos.toByteArray();
        JSONObject response = client.detect(bytes, new HashMap<String, String>());
        System.out.println(response.toString());
    }

    public void match(Bitmap bitmap1, Bitmap bitmap2) {
        ByteArrayOutputStream baos1 = new ByteArrayOutputStream();
        ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
        byte[][] bytes = new byte[2][];
        byte[] bytes1;
        byte[] bytes2;
        bitmap1.compress(Bitmap.CompressFormat.JPEG, 100, baos1);
        bitmap2.compress(Bitmap.CompressFormat.JPEG, 100, baos2);
        bytes1 = baos1.toByteArray();
        bytes2 = baos2.toByteArray();
        bytes[0] = bytes1;
        bytes[1] = bytes2;
        try {
            baos1.close();
            baos2.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        JSONObject response = client.match(bytes, new HashMap<String, String>());
        System.out.println(response.toString());
    }

}
